package controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import serivce.*;
import untity.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/houseDetail")
public class HouseDetail {

    @Autowired
    private House house;

    @Autowired
    private UserSerivce userSerivce;

    @Autowired
    private LoginService loginService;

    @Autowired
    AliPay aliPay;

    @Autowired
    OrderService orderService;


    @Autowired
    @Qualifier("chatList")
    List<ChatRoom> chatList;

    @ResponseBody
    @PostMapping("/pagedate")
    public JSONObject page(@RequestBody JSONObject data, HttpSession session){
        int id=data.getInteger("id");
        FangZhi f=house.getHouseById(id);
        JSONObject json=JSONObject.parseObject(JSON.toJSONString(f));
        String userid= String.valueOf(session.getAttribute("id"));
        System.out.println(userid);
        if(userid!="null"){
            LiShi l=new LiShi();
            Date date=new Date();
            System.out.println("date======"+date);
            l.setDate(date);
            l.setFangZhiId(id);
            l.setUserId(Integer.valueOf(userid));
            userSerivce.addHistory(l);
        }

        return json;
    }

    @ResponseBody
    @PostMapping("/nearby")
    public List<BriefHouse> nearbyHouse(@RequestBody Position position){
        System.out.println(position);
        List<BriefHouse> list= house.nearbyHouse(position);
        for (BriefHouse a:list) {
            System.out.println(a);
        }
        return list;
    }

    //此方法用来执行收藏操作
    @ResponseBody
    @PostMapping("/collection")
    public Boolean collection(@RequestBody HouseAndUser houseAndUser, HttpServletRequest request){
        HashMap<String,Object> map=loginService.checkSession(request);
        System.out.println(map);
        if (map==null){
            return null;
        }else {
            Integer a=(Integer) map.get("id");
            houseAndUser.setUserID(a);
        }
        System.out.println(houseAndUser);
        boolean f= userSerivce.collection(houseAndUser);
        return f;
    }


//    此方法来执行取消收藏操作
    @ResponseBody
    @PostMapping("/cancelCollection")
    public Boolean cancelCollection(@RequestBody HouseAndUser houseAndUser){
        Boolean flag=userSerivce.cancelCollection(houseAndUser);
        return flag;
    }

    //此方法用来查看当前用户是否已经收藏过该房源
    @ResponseBody
    @PostMapping("/checkCollection")
    public Boolean checkCollection(@RequestBody HouseAndUser houseAndUser){
        Boolean f=userSerivce.checkCollection(houseAndUser);
        return f;
    }

    @ResponseBody
    @PostMapping("/pay")
    public String pay(@RequestBody Order order,HttpSession session){
        String id=(String)session.getAttribute("id");
        if (id!=null){
            order.setUserID(Integer.valueOf(id));
            order.setDate(new Date());
            String body= aliPay.computerPay(order.getTotal().toString());
            orderService.creatOrder(order);
            house.updateStatus(""+order.getHouseID());
            return body;
        }else {
            return "0";
        }
    }

    @RequestMapping("/paydata")
    public String paydata(@RequestBody(required = false) JSONObject json,HttpSession session){
        System.out.println(json);
        System.out.println("!!!!");
        return null;
    }

    @ResponseBody
    @PostMapping("/connection")
    public String connection(@RequestBody JSONObject jsonObject,HttpSession session){
        String userid=(String) session.getAttribute("id");
        if (userid==null){
            return null;
        }
        String id=jsonObject.getString("houseID");

        UserConnection user=house.queryByhouseID(id);

        ChatRoom room=this.existRoom(Integer.valueOf(userid),Integer.valueOf(user.getId()));
        if (room!=null){
            return room.getId();
        }else {
            System.out.println(userid+"======="+user.getId());
            ChatRoomDB chatRoomDB=userSerivce.queryChatRoom(userid,user.getId());
            System.out.println(chatRoomDB);
            if(chatRoomDB!=null){
                return chatRoomDB.getId();
            }else {
                ChatRoomDB room1=new ChatRoomDB();
                room1.setLandlordId(user.getId());
                room1.setUserId(userid);
                room1.setPath("");
                userSerivce.creatConnection(room1);
                ChatRoom room2=new ChatRoom();
                room2.setId(room1.getId());
                room2.setUserId1(Integer.valueOf(userid));
                room2.setUserId2(Integer.valueOf(room1.getLandlordId()));
                chatList.add(room2);
                return room1.getId();
            }

        }

    }


    public ChatRoom existRoom(int userId,int landlordId){
        for (ChatRoom room :chatList) {
            if (room.getUserId1()==userId){
                if (room.getUserId2()==landlordId){
                    return room;
                }
            }
        }
        return null;
    }
}
